package com.zc.custom.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.zc.custom.domain.ChargeCustomOrderInfoDetails;
import com.zc.custom.mapper.ChargeCustomOrderInfoDetailsMapper;
import com.zc.operator.domain.ChargeStationAdjustPrice;
import com.zc.operator.domain.ChargeStationAdjustPriceAdditional;
import com.zc.operator.domain.ChargeStationPrice;
import com.zc.operator.domain.ChargeStationPriceType;
import com.zc.operator.mapper.ChargeStationAdjustPriceMapper;
import com.zc.operator.mapper.ChargeStationPriceMapper;
import com.zc.operator.mapper.ChargeStationPriceTypeMapper;
import com.zc.util.ArithmeticUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zc.custom.mapper.ChargeCustomOrderInfoMapper;
import com.zc.custom.domain.ChargeCustomOrderInfo;
import com.zc.custom.service.IChargeCustomOrderInfoService;

/**
 * 用户订单Service业务层处理
 *
 * @author glb
 * @date 2023-05-19
 */
@Service
public class ChargeCustomOrderInfoServiceImpl implements IChargeCustomOrderInfoService {
    @Autowired
    private ChargeCustomOrderInfoMapper chargeCustomOrderInfoMapper;
    @Autowired
    private ChargeStationPriceMapper chargeStationPriceMapper;
    @Autowired
    private ChargeStationPriceTypeMapper chargeStationPriceTypeMapper;
    @Autowired
    private ChargeCustomOrderInfoDetailsMapper chargeCustomOrderInfoDetailsMapper;

    private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private ChargeStationAdjustPriceMapper chargeStationAdjustPriceMapper;

    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    /**
     * 查询用户订单
     *
     * @param orderInfoId 用户订单主键
     * @return 用户订单
     */
    @Override
    @DataScope(deptAlias = "d")
    public ChargeCustomOrderInfo selectChargeCustomOrderInfoByOrderInfoId(Long orderInfoId) {
        ChargeCustomOrderInfo chargeCustomOrderInfo = chargeCustomOrderInfoMapper.selectChargeCustomOrderInfoByOrderInfoId(orderInfoId);
        return chargeCustomOrderInfo;
    }

    @Override
    public HashMap getInfoCount() {
        List<HashMap> infoCount = chargeCustomOrderInfoMapper.getInfoCount();
        HashMap maps = new HashMap();
        for (HashMap hashMap : infoCount) {
            maps.put(hashMap.get("orderStatus"), hashMap.get("number"));
        }
        HashMap map = new HashMap();
        int one = Integer.parseInt(maps.containsKey("1") ? String.valueOf(maps.get("1")) : "0");
        int two = Integer.parseInt(maps.containsKey("2") ? String.valueOf(maps.get("2")) : "0");
        int three = Integer.parseInt(maps.containsKey("3") ? String.valueOf(maps.get("3")) : "0");
        int four = Integer.parseInt(maps.containsKey("4") ? String.valueOf(maps.get("4")) : "0");
        map.put("countOrder", one + two + three + four);
        map.put("haveOrder", one);
        map.put("completedOrder", two);
        return map;
    }

    /**
     * 查询用户订单列表
     *
     * @param chargeCustomOrderInfo 用户订单
     * @return 用户订单
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ChargeCustomOrderInfo> selectChargeCustomOrderInfoList(ChargeCustomOrderInfo chargeCustomOrderInfo) {
        List<ChargeCustomOrderInfo> chargeCustomOrderInfos = chargeCustomOrderInfoMapper.selectChargeCustomOrderInfoList(chargeCustomOrderInfo);
        for (ChargeCustomOrderInfo c : chargeCustomOrderInfos) {
            if (c.getChargeInfo() != null) {
                List<String> timeList = new ArrayList<>();
                List<String> outVList = new ArrayList<>();
                List<String> needVList = new ArrayList<>();
                List<String> outIList = new ArrayList<>();
                List<String> needIList = new ArrayList<>();
                List<String> powerList = new ArrayList<>();
                List<String> SOCList = new ArrayList<>();
                String chargeInfo = c.getChargeInfo();
                if (!StringUtils.isEmpty(chargeInfo)) {
                    JSONObject jsonObject = JSONObject.parseObject(chargeInfo);
                    //组装数据
                    Map<String, Map<String, Object>> map = jsonObject.toJavaObject(Map.class);
                    Map<String, Map<String, Object>> result = new LinkedHashMap<>();
                    map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> result.put(x.getKey(), x.getValue()));
                    for (String key : result.keySet()) {
                        if (map.get(key).get("needV") != null) {
                            if (!StringUtils.isEmpty(key)) {
                                String substringKey = key.substring(11);
                                timeList.add(substringKey);
                            }
                            if (map.get(key).get("outV") != null) {
                                outVList.add(map.get(key).get("outV").toString());
                            }
                            if (map.get(key).get("needV") != null) {
                                needVList.add(map.get(key).get("needV").toString());
                            }
                            if (map.get(key).get("outI") != null) {
                                outIList.add(map.get(key).get("outI").toString());
                            }
                            if (map.get(key).get("needI") != null) {
                                needIList.add(map.get(key).get("needI").toString());
                            }
                            if (map.get(key).get("chargeKwh") != null) {
                                powerList.add(map.get(key).get("chargeKwh").toString());
                            }
                            if (map.get(key).get("SOC") != null) {
                                SOCList.add(map.get(key).get("SOC").toString());
                            }
                        }
                    }
                    c.setTimeList(timeList);
                    c.setOutVList(outVList);
                    c.setOutIList(outIList);
                    c.setNeedVList(needVList);
                    c.setNeedIList(needIList);
                    c.setPowerList(powerList);
                    c.setSocList(SOCList);
                }
            }
        }
        return chargeCustomOrderInfos;
    }

    /**
     * 新增用户订单
     *
     * @param chargeCustomOrderInfo 用户订单
     * @return 结果
     */
    @Override
    public int insertChargeCustomOrderInfo(ChargeCustomOrderInfo chargeCustomOrderInfo) {
        return chargeCustomOrderInfoMapper.insertChargeCustomOrderInfo(chargeCustomOrderInfo);
    }

    /**
     * 修改用户订单
     *
     * @param chargeCustomOrderInfo 用户订单
     * @return 结果
     */
    @Override
    public int updateChargeCustomOrderInfo(ChargeCustomOrderInfo chargeCustomOrderInfo) {
        return chargeCustomOrderInfoMapper.updateChargeCustomOrderInfo(chargeCustomOrderInfo);
    }

    /**
     * 批量删除用户订单
     *
     * @param orderInfoIds 需要删除的用户订单主键
     * @return 结果
     */
    @Override
    public int deleteChargeCustomOrderInfoByOrderInfoIds(Long[] orderInfoIds) {
        return chargeCustomOrderInfoMapper.deleteChargeCustomOrderInfoByOrderInfoIds(orderInfoIds);
    }

    /**
     * 删除用户订单信息
     *
     * @param orderInfoId 用户订单主键
     * @return 结果
     */
    @Override
    public int deleteChargeCustomOrderInfoByOrderInfoId(Long orderInfoId) {
        return chargeCustomOrderInfoMapper.deleteChargeCustomOrderInfoByOrderInfoId(orderInfoId);
    }

    /**
     * 获取指定时间段内设备充电记录
     *
     * @param deviceCode
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<ChargeCustomOrderInfo> getOrderList(String deviceCode, String portId, Date startDate, Date endDate) {
        return chargeCustomOrderInfoMapper.getOrderList(deviceCode, portId, startDate, endDate);
    }

    @Override
    public List<ChargeCustomOrderInfoDetails> getOrderInfoDetailsList(ChargeCustomOrderInfo chargeCustomOrderInfo) {
        List<ChargeCustomOrderInfoDetails> list = new ArrayList<>();
        if (chargeCustomOrderInfo.getStartTime() != null && chargeCustomOrderInfo.getEndTime() != null) {
            Map<String, Integer> hourMinute = getHourMinute(chargeCustomOrderInfo);
            Map<String, Map<String, BigDecimal>> hourPrice = getHourPrice(chargeCustomOrderInfo.getPriceTypeCode());
            int between = (int) DateUtil.between(chargeCustomOrderInfo.getStartTime(), chargeCustomOrderInfo.getEndTime(), DateUnit.MINUTE);
            BigDecimal totalPower = chargeCustomOrderInfo.getTotalPower();
            double power = ArithmeticUtils.div(totalPower.doubleValue(), between);
            String startTime = sdf.format(chargeCustomOrderInfo.getStartTime());
            String endTime = sdf.format(chargeCustomOrderInfo.getEndTime());
            int startNumber = Integer.parseInt(startTime.substring(0, 2));
            int endNumber = Integer.parseInt(endTime.substring(0, 2));
            BigDecimal totalPowerMoney = BigDecimal.valueOf(0d);
            BigDecimal totalServiceMoney = BigDecimal.valueOf(0d);
            BigDecimal totalizeMoney = BigDecimal.valueOf(0d);
            if (startNumber > endNumber) {
                endNumber = endNumber + 24;
            }
            while (startNumber <= endNumber) {
                int hour = startNumber;
                if (startNumber >= 24) {
                    hour = startNumber - 24;
                }
                Map<String, BigDecimal> map = hourPrice.get(String.valueOf(hour));
                String electricCharge = String.valueOf(map.get("electricCharge"));
                String serviceFee = String.valueOf(map.get("serviceFee"));
                ChargeCustomOrderInfoDetails chargeCustomOrderInfoDetails = new ChargeCustomOrderInfoDetails();
                Integer totalTime = hourMinute.get(String.valueOf(hour));
                double countPower = ArithmeticUtils.mul(totalTime, power, 2);
                double totalMoney = ArithmeticUtils.mul(countPower, map.get("electricCharge").doubleValue(), 2);
                double totalServeMoney = ArithmeticUtils.mul(countPower, map.get("serviceFee").doubleValue(), 2);
                totalPowerMoney = BigDecimal.valueOf(ArithmeticUtils.add(totalPowerMoney.doubleValue(), countPower));
                totalServiceMoney = BigDecimal.valueOf(ArithmeticUtils.add(totalServiceMoney.doubleValue(), totalServeMoney));
                String start = String.valueOf(hour);
                String end = String.valueOf(hour + 1);
                if (hour < 10) {
                    start = "0" + String.valueOf(hour);
                }
                if (hour + 1 < 10) {
                    end = "0" + String.valueOf(hour + 1);
                }
                chargeCustomOrderInfoDetails.setOrderInfoCode(chargeCustomOrderInfo.getOrderInfoCode());
                chargeCustomOrderInfoDetails.setTotalPower(BigDecimal.valueOf(countPower));
                chargeCustomOrderInfoDetails.setElectricChargeUnivalent(new BigDecimal(electricCharge));
                chargeCustomOrderInfoDetails.setTotalMoney(BigDecimal.valueOf(totalMoney));
                chargeCustomOrderInfoDetails.setServiceChargeUnivalent(new BigDecimal(serviceFee));
                chargeCustomOrderInfoDetails.setTotalServeMoney(BigDecimal.valueOf(totalServeMoney));
                chargeCustomOrderInfoDetails.setTimeSlot(start + ":00" + "-" + end + ":00");
                chargeCustomOrderInfoDetails.setTotalTime(totalTime);
                list.add(chargeCustomOrderInfoDetails);
                startNumber = startNumber + 1;
            }
            totalizeMoney = BigDecimal.valueOf(ArithmeticUtils.add(totalPowerMoney.doubleValue(), totalServiceMoney.doubleValue()));
            chargeCustomOrderInfo.setTotalElecMoney(totalPowerMoney);
            chargeCustomOrderInfo.setTotalSeviceMoney(chargeCustomOrderInfo.getChargeMoney().subtract(totalPowerMoney));
            chargeCustomOrderInfo.setTotalMoney(totalizeMoney);
            int i = chargeCustomOrderInfoMapper.updateChargeCustomOrderInfo(chargeCustomOrderInfo);
            chargeCustomOrderInfoDetailsMapper.deleteChargeCustomOrderInfoDetailsByOrderInfoCode(chargeCustomOrderInfo.getOrderInfoCode());
            chargeCustomOrderInfoDetailsMapper.batchAddChargeCustomOrderInfoDetails(list);
            return list;
        }
        return list;

    }

    @Override
    public void/*ChargeCustomOrderInfo*/ getOrderPriceDetails(ChargeCustomOrderInfo chargeCustomOrderInfo) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间 结束时间
        if (chargeCustomOrderInfo.getStartTime() != null && chargeCustomOrderInfo.getEndTime() != null) {
            //价格
            List<Map<String, Object>> hourPrice = getHourPriceNew(chargeCustomOrderInfo.getPriceTypeCode());
            //充电分钟数
            int chargeMinute = (int) DateUtil.between(chargeCustomOrderInfo.getStartTime(), chargeCustomOrderInfo.getEndTime(), DateUnit.MINUTE);
//            chargeMinute = chargeCustomOrderInfo.getTotalTime().intValue();
            //充电电量
            BigDecimal totalPower = chargeCustomOrderInfo.getTotalPower();
            //每分钟电量
            BigDecimal powerMinute = totalPower.divide(new BigDecimal(chargeMinute), 10, BigDecimal.ROUND_HALF_UP);
            //开始时间
            String startTime = sdf.format(chargeCustomOrderInfo.getStartTime());
            //结束时间
            String endTime = sdf.format(chargeCustomOrderInfo.getEndTime());
            //电价
            BigDecimal electricityPrice = BigDecimal.ZERO;
            //服务费
            BigDecimal serviceCharge = BigDecimal.ZERO;
            //计算
            if (Integer.parseInt(startTime.substring(11, 13)) > Integer.parseInt(endTime.substring(11, 13))) {
                //1.隔天
                //跨区间
                int i = Integer.parseInt(endTime.substring(11, 13)) + 24;
                String endTimeAdd = endTime.substring(0, 11) + i + endTime.substring(13);
                /*chargeCustomOrderInfo = */
                calculatePrice(hourPrice, startTime, endTimeAdd, chargeCustomOrderInfo, electricityPrice, serviceCharge, powerMinute, sdf);
            } else {
                //2.不隔天
                //都在一个价格区间内
                for (Map<String, Object> stringObjectMap : hourPrice) {
                    String time = String.valueOf(stringObjectMap.get("time"));
                    String[] split = time.split(",");
                    if ((split[0].compareTo(startTime.substring(11, 16)) < 0 || split[0].compareTo(startTime.substring(11, 16)) == 0) && endTime.substring(11, 16).compareTo(split[1]) < 0) {
                        electricityPrice = new BigDecimal(String.valueOf(stringObjectMap.get("electricityPrice"))).multiply(totalPower);
                        serviceCharge = new BigDecimal(String.valueOf(stringObjectMap.get("serviceCharge"))).multiply(totalPower);
                        log.error("++++++++++++++++++++++++++++交易订单,结束充电,电费为++++++++++++++++++++++++++++>" + electricityPrice);
                        log.error("++++++++++++++++++++++++++++交易订单,结束充电,服务费为++++++++++++++++++++++++++++>" + serviceCharge);
                        log.error("++++++++++++++++++++++++++++交易订单,结束充电,总价为++++++++++++++++++++++++++++>" + electricityPrice.add(serviceCharge));
//                        chargeCustomOrderInfo.setTotalElecMoney(electricityPrice);
//                        chargeCustomOrderInfo.setTotalSeviceMoney(serviceCharge);
//                        chargeCustomOrderInfo.setChargeMoney(electricityPrice.add(serviceCharge));
//                        return chargeCustomOrderInfo;
                    }
                }
                //跨区间
                /*chargeCustomOrderInfo = */
                calculatePrice(hourPrice, startTime, endTime, chargeCustomOrderInfo, electricityPrice, serviceCharge, powerMinute, sdf);
            }

//            return chargeCustomOrderInfo;
        }
//        return chargeCustomOrderInfo;

    }

    //跨区间计算价格
    public void/*ChargeCustomOrderInfo*/ calculatePrice(List<Map<String, Object>> hourPrice, String startTime, String endTime,
                                                        ChargeCustomOrderInfo chargeCustomOrderInfo, BigDecimal electricityPrice,
                                                        BigDecimal serviceCharge, BigDecimal powerMinute, SimpleDateFormat sdf) throws Exception {
        String endTimeSub = endTime.substring(11, 13);
        if (Integer.parseInt(endTimeSub) > 23) {
            Integer i = Integer.parseInt(endTimeSub) - 24;
            if (i < 10) {
                endTimeSub = endTime.substring(0, 11) + "0" + i + endTime.substring(13);
            } else {
                endTimeSub = endTime.substring(0, 11) + i + endTime.substring(13);
            }
        }

        //跨区间
        for (int i = 0; i < hourPrice.size(); i++) {
            Map<String, Object> stringObjectMap = hourPrice.get(i);
            String time = String.valueOf(stringObjectMap.get("time"));
            String[] split = time.split(",");

            BigDecimal electricityPriceNow = new BigDecimal(String.valueOf(stringObjectMap.get("electricityPrice")));
            BigDecimal serviceChargeNow = new BigDecimal(String.valueOf(stringObjectMap.get("serviceCharge")));

            if ((split[0].compareTo(startTime.substring(11, 16)) < 0 || split[0].compareTo(startTime.substring(11, 16)) == 0)
                    && startTime.substring(11, 16).compareTo(split[1]) < 0 && endTime.substring(11, 16).compareTo(split[1]) > 0) {
                //开始时间(隔天+不隔天)
                String end = "";
                if (Integer.parseInt(endTime.substring(11, 13)) > 23) {
                    end = startTime.substring(0, 11) + split[1] + ":00";
                } else {
                    end = endTime.substring(0, 11) + split[1] + ":00";
                }
                int diffMin = (int) DateUtil.between(chargeCustomOrderInfo.getStartTime(), sdf.parse(end), DateUnit.MINUTE);
                //电价
                electricityPrice = electricityPrice.add(electricityPriceNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                //服务费
                serviceCharge = serviceCharge.add(serviceChargeNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
            } else if (Integer.parseInt(endTime.substring(11, 13)) > 23) {
                if ((split[0].compareTo(endTimeSub.substring(11, 16)) < 0 || split[0].compareTo(endTimeSub.substring(11, 16)) == 0)
                        && endTimeSub.substring(11, 16).compareTo(split[1]) > 0) {
                    //包含区间(前段 隔天)
                    String start = endTime.substring(0, 11) + split[0] + ":00";
                    String end = endTime.substring(0, 11) + split[1] + ":00";
                    int diffMin = (int) DateUtil.between(sdf.parse(start), sdf.parse(end), DateUnit.MINUTE);
                    //电价
                    electricityPrice = electricityPrice.add(electricityPriceNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                    //服务费
                    serviceCharge = serviceCharge.add(serviceChargeNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                } else if ((split[0].compareTo(endTimeSub.substring(11, 16)) < 0 || split[0].compareTo(endTimeSub.substring(11, 16)) == 0)
                        && endTimeSub.substring(11, 16).compareTo(split[1]) < 0) {
                    //结束时间(隔天)
                    String start = endTimeSub.substring(0, 11) + split[0] + ":00";
                    int diffMin = (int) DateUtil.between(sdf.parse(start), sdf.parse(endTimeSub), DateUnit.MINUTE);
                    //电价
                    electricityPrice = electricityPrice.add(electricityPriceNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                    //服务费
                    serviceCharge = serviceCharge.add(serviceChargeNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                } else if (split[0].compareTo(startTime.substring(11, 16)) > 0 && endTime.substring(11, 16).compareTo(split[1]) > 0) {
                    //包含区间(后段 隔天)
                    String start = startTime.substring(0, 11) + split[0] + ":00";
                    String end = startTime.substring(0, 11) + split[1] + ":00";
                    int diffMin = (int) DateUtil.between(sdf.parse(start), sdf.parse(end), DateUnit.MINUTE);
                    //电价
                    electricityPrice = electricityPrice.add(electricityPriceNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                    //服务费
                    serviceCharge = serviceCharge.add(serviceChargeNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                }
            } else if (split[0].compareTo(startTime.substring(11, 16)) > 0 && endTime.substring(11, 16).compareTo(split[1]) > 0) {
                //包含区间(后段 不隔天)
                String start = startTime.substring(0, 11) + split[0] + ":00";
                String end = endTime.substring(0, 11) + split[1] + ":00";
                int diffMin = (int) DateUtil.between(sdf.parse(start), sdf.parse(end), DateUnit.MINUTE);
                //电价
                electricityPrice = electricityPrice.add(electricityPriceNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                //服务费
                serviceCharge = serviceCharge.add(serviceChargeNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
            } else if (split[0].compareTo(startTime.substring(11, 16)) > 0 && split[0].compareTo(endTime.substring(11, 16)) < 0
                    && split[1].compareTo(endTime.substring(11, 16)) > 0 && Integer.parseInt(endTime.substring(11, 13)) < 24) {
                //结束时间(不隔天)
                String start = endTime.substring(0, 11) + split[0] + ":00";
                int diffMin = (int) DateUtil.between(sdf.parse(start), chargeCustomOrderInfo.getEndTime(), DateUnit.MINUTE);
                //电价
                electricityPrice = electricityPrice.add(electricityPriceNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
                //服务费
                serviceCharge = serviceCharge.add(serviceChargeNow.multiply(new BigDecimal(diffMin).multiply(powerMinute)));
            }
        }

        electricityPrice = electricityPrice.setScale(2, BigDecimal.ROUND_HALF_UP);
        serviceCharge = serviceCharge.setScale(2, BigDecimal.ROUND_HALF_UP);
        log.error("++++++++++++++++++++++++++++交易订单,结束充电,电费为++++++++++++++++++++++++++++>" + electricityPrice);
        log.error("++++++++++++++++++++++++++++交易订单,结束充电,服务费为++++++++++++++++++++++++++++>" + serviceCharge);
        log.error("++++++++++++++++++++++++++++交易订单,结束充电,总价为++++++++++++++++++++++++++++>" + electricityPrice.add(serviceCharge));
//        chargeCustomOrderInfo.setTotalElecMoney(electricityPrice);
//        chargeCustomOrderInfo.setTotalSeviceMoney(serviceCharge);
//        chargeCustomOrderInfo.setChargeMoney(electricityPrice.add(serviceCharge));
//        return chargeCustomOrderInfo;
    }

    @Override
    public Map<String, Object> getOrderPriceList(ChargeCustomOrderInfo chargeCustomOrderInfo) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> returnMap = new HashMap<>();
        //开始时间 结束时间
        if (chargeCustomOrderInfo.getStartTime() != null && chargeCustomOrderInfo.getEndTime() != null) {
            //价格
            List<Map<String, Object>> hourPrice = getHourPriceNew(chargeCustomOrderInfo.getPriceTypeCode());
            //充电分钟数
            int chargeMinute = (int) DateUtil.between(chargeCustomOrderInfo.getStartTime(), chargeCustomOrderInfo.getEndTime(), DateUnit.MINUTE);
//            chargeMinute = chargeCustomOrderInfo.getTotalTime().intValue();
            //充电电量
            BigDecimal totalPower = chargeCustomOrderInfo.getTotalPower();
            //每分钟电量
            BigDecimal powerMinute = totalPower.divide(new BigDecimal(chargeMinute), 10, BigDecimal.ROUND_HALF_UP);
            //开始时间
            String startTime = sdf.format(chargeCustomOrderInfo.getStartTime());
            //结束时间
            String endTime = sdf.format(chargeCustomOrderInfo.getEndTime());
            //电价
            BigDecimal electricityPrice = BigDecimal.ZERO;
            //服务费
            BigDecimal serviceCharge = BigDecimal.ZERO;
            //计算
            if (Integer.parseInt(startTime.substring(11, 13)) > Integer.parseInt(endTime.substring(11, 13))) {
                //1.隔天
                //跨区间
                int i = Integer.parseInt(endTime.substring(11, 13)) + 24;
                String endTimeAdd = endTime.substring(0, 11) + i + endTime.substring(13);
                Map<String, Object> SumPeriodMap = calculatePriceList(hourPrice, startTime, endTimeAdd, chargeCustomOrderInfo, electricityPrice, serviceCharge, powerMinute, sdf);
                return SumPeriodMap;
            } else {
                //2.不隔天
                //都在一个价格区间内
                for (Map<String, Object> stringObjectMap : hourPrice) {
                    String time = String.valueOf(stringObjectMap.get("time"));
                    String[] split = time.split(",");
                    if ((split[0].compareTo(startTime.substring(11, 16)) < 0 || split[0].compareTo(startTime.substring(11, 16)) == 0) && endTime.substring(11, 16).compareTo(split[1]) < 0) {
                        electricityPrice = new BigDecimal(String.valueOf(stringObjectMap.get("electricityPrice"))).multiply(totalPower);
                        serviceCharge = new BigDecimal(String.valueOf(stringObjectMap.get("serviceCharge"))).multiply(totalPower);

                        ArrayList<Map<String, Object>> lists = new ArrayList<>();
                        HashMap<String, Object> SumPeriodMap = new HashMap<>();
                        SumPeriodMap.put("DetailsStartTime", startTime);//开始时间 格式（yyyy-MM-dd HH:mm:ss）
                        SumPeriodMap.put("DetailsEndTime", endTime);//结束时间 格式（yyyy-MM-dd HH:mm:ss）
                        SumPeriodMap.put("ElecPrice", new BigDecimal(String.valueOf(stringObjectMap.get("electricityPrice"))).setScale(4, BigDecimal.ROUND_HALF_UP));//时段电价 小数点后4位 格式（0.00）
                        SumPeriodMap.put("ServicePrice", new BigDecimal(String.valueOf(stringObjectMap.get("serviceCharge"))).setScale(4, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后4位 格式（0.00）
                        SumPeriodMap.put("DetailPower", totalPower.setScale(2, BigDecimal.ROUND_HALF_UP));//时段充电量 单位:度 小数点后2位 格式（0.00）
                        SumPeriodMap.put("DetailElecMoney", electricityPrice.setScale(2, BigDecimal.ROUND_HALF_UP));//时段电费 小数点后2位 格式（0.00）
                        SumPeriodMap.put("DetailServiceMoney", serviceCharge.setScale(2, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后2位 格式（0.00）
                        lists.add(SumPeriodMap);
                        returnMap.put("SumPeriod", 1);
                        returnMap.put("ChargeDetails", lists);
                        returnMap.put("electricityPrice", electricityPrice.setScale(2, BigDecimal.ROUND_HALF_UP));
                        returnMap.put("serviceCharge", serviceCharge.setScale(2, BigDecimal.ROUND_HALF_UP));
                        return returnMap;
                    }
                }
                //跨区间
                Map<String, Object> SumPeriodMap = calculatePriceList(hourPrice, startTime, endTime, chargeCustomOrderInfo, electricityPrice, serviceCharge, powerMinute, sdf);
                return SumPeriodMap;
            }
        }

        return null;
    }

    //跨区间计算价格
    public Map<String, Object> calculatePriceList(List<Map<String, Object>> hourPrice, String startTime, String endTime,
                                                  ChargeCustomOrderInfo chargeCustomOrderInfo, BigDecimal electricityPrice,
                                                  BigDecimal serviceCharge, BigDecimal powerMinute, SimpleDateFormat sdf) throws Exception {

        int SumPeriod = 0;

        Map<String, Object> returnMap = new HashMap<>();
        ArrayList<Map<String, Object>> lists = new ArrayList<>();

        String endTimeSub = endTime.substring(11, 13);
        if (Integer.parseInt(endTimeSub) > 23) {
            Integer i = Integer.parseInt(endTimeSub) - 24;
            if (i < 10) {
                endTimeSub = endTime.substring(0, 11) + "0" + i + endTime.substring(13);
            } else {
                endTimeSub = endTime.substring(0, 11) + i + endTime.substring(13);
            }
        }

        //跨区间
        for (int i = 0; i < hourPrice.size(); i++) {
            Map<String, Object> stringObjectMap = hourPrice.get(i);
            String time = String.valueOf(stringObjectMap.get("time"));
            String[] split = time.split(",");

            BigDecimal electricityPriceNow = new BigDecimal(String.valueOf(stringObjectMap.get("electricityPrice")));
            BigDecimal serviceChargeNow = new BigDecimal(String.valueOf(stringObjectMap.get("serviceCharge")));

            if ((split[0].compareTo(startTime.substring(11, 16)) < 0 || split[0].compareTo(startTime.substring(11, 16)) == 0)
                    && startTime.substring(11, 16).compareTo(split[1]) < 0 && endTime.substring(11, 16).compareTo(split[1]) > 0) {
                //开始时间(隔天+不隔天)
                String end = "";
                if (Integer.parseInt(endTime.substring(11, 13)) > 23) {
                    end = startTime.substring(0, 11) + split[1] + ":00";
                } else {
                    end = endTime.substring(0, 11) + split[1] + ":00";
                }
                int diffMin = (int) DateUtil.between(chargeCustomOrderInfo.getStartTime(), sdf.parse(end), DateUnit.MINUTE);
                //该时段电量
                BigDecimal allPower = new BigDecimal(diffMin).multiply(powerMinute);
                //该时段电价
                BigDecimal electricityPriceTime = electricityPriceNow.multiply(allPower);
                //该时段服务费
                BigDecimal serviceChargeTime = serviceChargeNow.multiply(allPower);
                SumPeriod++;
                HashMap<String, Object> SumPeriodMap = new HashMap<>();
                SumPeriodMap.put("DetailsStartTime", startTime);//开始时间 格式（yyyy-MM-dd HH:mm:ss）
                SumPeriodMap.put("DetailsEndTime", endTime);//结束时间 格式（yyyy-MM-dd HH:mm:ss）
                SumPeriodMap.put("ElecPrice", electricityPriceNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段电价 小数点后4位 格式（0.00）
                SumPeriodMap.put("ServicePrice", serviceChargeNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后4位 格式（0.00）
                SumPeriodMap.put("DetailPower", allPower.setScale(2, BigDecimal.ROUND_HALF_UP));//时段充电量 单位:度 小数点后2位 格式（0.00）
                SumPeriodMap.put("DetailElecMoney", electricityPriceTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段电费 小数点后2位 格式（0.00）
                SumPeriodMap.put("DetailServiceMoney", serviceChargeTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后2位 格式（0.00）
                lists.add(SumPeriodMap);
                //总电价
                electricityPrice = electricityPrice.add(electricityPriceTime);
                //总服务费
                serviceCharge = serviceCharge.add(serviceChargeTime);

            } else if (Integer.parseInt(endTime.substring(11, 13)) > 23) {
                if ((split[0].compareTo(endTimeSub.substring(11, 16)) < 0 || split[0].compareTo(endTimeSub.substring(11, 16)) == 0)
                        && endTimeSub.substring(11, 16).compareTo(split[1]) > 0) {
                    //包含区间(前段 隔天)
                    String start = endTime.substring(0, 11) + split[0] + ":00";
                    String end = endTime.substring(0, 11) + split[1] + ":00";
                    int diffMin = (int) DateUtil.between(sdf.parse(start), sdf.parse(end), DateUnit.MINUTE);
                    //该时段电量
                    BigDecimal allPower = new BigDecimal(diffMin).multiply(powerMinute);
                    //该时段电价
                    BigDecimal electricityPriceTime = electricityPriceNow.multiply(allPower);
                    //该时段服务费
                    BigDecimal serviceChargeTime = serviceChargeNow.multiply(allPower);
                    SumPeriod++;
                    HashMap<String, Object> SumPeriodMap = new HashMap<>();
                    SumPeriodMap.put("DetailsStartTime", startTime);//开始时间 格式（yyyy-MM-dd HH:mm:ss）
                    SumPeriodMap.put("DetailsEndTime", endTime);//结束时间 格式（yyyy-MM-dd HH:mm:ss）
                    SumPeriodMap.put("ElecPrice", electricityPriceNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段电价 小数点后4位 格式（0.00）
                    SumPeriodMap.put("ServicePrice", serviceChargeNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后4位 格式（0.00）
                    SumPeriodMap.put("DetailPower", allPower.setScale(2, BigDecimal.ROUND_HALF_UP));//时段充电量 单位:度 小数点后2位 格式（0.00）
                    SumPeriodMap.put("DetailElecMoney", electricityPriceTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段电费 小数点后2位 格式（0.00）
                    SumPeriodMap.put("DetailServiceMoney", serviceChargeTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后2位 格式（0.00）
                    lists.add(SumPeriodMap);
                //总电价
                electricityPrice = electricityPrice.add(electricityPriceTime);
                //总服务费
                serviceCharge = serviceCharge.add(serviceChargeTime);
                } else if ((split[0].compareTo(endTimeSub.substring(11, 16)) < 0 || split[0].compareTo(endTimeSub.substring(11, 16)) == 0)
                        && endTimeSub.substring(11, 16).compareTo(split[1]) < 0) {
                    //结束时间(隔天)
                    String start = endTimeSub.substring(0, 11) + split[0] + ":00";
                    int diffMin = (int) DateUtil.between(sdf.parse(start), sdf.parse(endTimeSub), DateUnit.MINUTE);
                    //该时段电量
                    BigDecimal allPower = new BigDecimal(diffMin).multiply(powerMinute);
                    //该时段电价
                    BigDecimal electricityPriceTime = electricityPriceNow.multiply(allPower);
                    //该时段服务费
                    BigDecimal serviceChargeTime = serviceChargeNow.multiply(allPower);
                    SumPeriod++;
                    HashMap<String, Object> SumPeriodMap = new HashMap<>();
                    SumPeriodMap.put("DetailsStartTime", startTime);//开始时间 格式（yyyy-MM-dd HH:mm:ss）
                    SumPeriodMap.put("DetailsEndTime", endTime);//结束时间 格式（yyyy-MM-dd HH:mm:ss）
                    SumPeriodMap.put("ElecPrice", electricityPriceNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段电价 小数点后4位 格式（0.00）
                    SumPeriodMap.put("ServicePrice", serviceChargeNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后4位 格式（0.00）
                    SumPeriodMap.put("DetailPower", allPower.setScale(2, BigDecimal.ROUND_HALF_UP));//时段充电量 单位:度 小数点后2位 格式（0.00）
                    SumPeriodMap.put("DetailElecMoney", electricityPriceTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段电费 小数点后2位 格式（0.00）
                    SumPeriodMap.put("DetailServiceMoney", serviceChargeTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后2位 格式（0.00）
                    lists.add(SumPeriodMap);
                //总电价
                electricityPrice = electricityPrice.add(electricityPriceTime);
                //总服务费
                serviceCharge = serviceCharge.add(serviceChargeTime);
                } else if (split[0].compareTo(startTime.substring(11, 16)) > 0 && endTime.substring(11, 16).compareTo(split[1]) > 0) {
                    //包含区间(后段 隔天)
                    String start = startTime.substring(0, 11) + split[0] + ":00";
                    String end = startTime.substring(0, 11) + split[1] + ":00";
                    int diffMin = (int) DateUtil.between(sdf.parse(start), sdf.parse(end), DateUnit.MINUTE);
                    //该时段电量
                    BigDecimal allPower = new BigDecimal(diffMin).multiply(powerMinute);
                    //该时段电价
                    BigDecimal electricityPriceTime = electricityPriceNow.multiply(allPower);
                    //该时段服务费
                    BigDecimal serviceChargeTime = serviceChargeNow.multiply(allPower);
                    SumPeriod++;
                    HashMap<String, Object> SumPeriodMap = new HashMap<>();
                    SumPeriodMap.put("DetailsStartTime", startTime);//开始时间 格式（yyyy-MM-dd HH:mm:ss）
                    SumPeriodMap.put("DetailsEndTime", endTime);//结束时间 格式（yyyy-MM-dd HH:mm:ss）
                    SumPeriodMap.put("ElecPrice", electricityPriceNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段电价 小数点后4位 格式（0.00）
                    SumPeriodMap.put("ServicePrice", serviceChargeNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后4位 格式（0.00）
                    SumPeriodMap.put("DetailPower", allPower.setScale(2, BigDecimal.ROUND_HALF_UP));//时段充电量 单位:度 小数点后2位 格式（0.00）
                    SumPeriodMap.put("DetailElecMoney", electricityPriceTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段电费 小数点后2位 格式（0.00）
                    SumPeriodMap.put("DetailServiceMoney", serviceChargeTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后2位 格式（0.00）
                    lists.add(SumPeriodMap);
                //总电价
                electricityPrice = electricityPrice.add(electricityPriceTime);
                //总服务费
                serviceCharge = serviceCharge.add(serviceChargeTime);
                }
            } else if (split[0].compareTo(startTime.substring(11, 16)) > 0 && endTime.substring(11, 16).compareTo(split[1]) > 0) {
                //包含区间(后段 不隔天)
                String start = startTime.substring(0, 11) + split[0] + ":00";
                String end = endTime.substring(0, 11) + split[1] + ":00";
                int diffMin = (int) DateUtil.between(sdf.parse(start), sdf.parse(end), DateUnit.MINUTE);
                //该时段电量
                BigDecimal allPower = new BigDecimal(diffMin).multiply(powerMinute);
                //该时段电价
                BigDecimal electricityPriceTime = electricityPriceNow.multiply(allPower);
                //该时段服务费
                BigDecimal serviceChargeTime = serviceChargeNow.multiply(allPower);
                SumPeriod++;
                HashMap<String, Object> SumPeriodMap = new HashMap<>();
                SumPeriodMap.put("DetailsStartTime", startTime);//开始时间 格式（yyyy-MM-dd HH:mm:ss）
                SumPeriodMap.put("DetailsEndTime", endTime);//结束时间 格式（yyyy-MM-dd HH:mm:ss）
                SumPeriodMap.put("ElecPrice", electricityPriceNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段电价 小数点后4位 格式（0.00）
                SumPeriodMap.put("ServicePrice", serviceChargeNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后4位 格式（0.00）
                SumPeriodMap.put("DetailPower", allPower.setScale(2, BigDecimal.ROUND_HALF_UP));//时段充电量 单位:度 小数点后2位 格式（0.00）
                SumPeriodMap.put("DetailElecMoney", electricityPriceTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段电费 小数点后2位 格式（0.00）
                SumPeriodMap.put("DetailServiceMoney", serviceChargeTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后2位 格式（0.00）
                lists.add(SumPeriodMap);
                //总电价
                electricityPrice = electricityPrice.add(electricityPriceTime);
                //总服务费
                serviceCharge = serviceCharge.add(serviceChargeTime);
            } else if (split[0].compareTo(startTime.substring(11, 16)) > 0 && split[0].compareTo(endTime.substring(11, 16)) < 0
                    && split[1].compareTo(endTime.substring(11, 16)) > 0 && Integer.parseInt(endTime.substring(11, 13)) < 24) {
                //结束时间(不隔天)
                String start = endTime.substring(0, 11) + split[0] + ":00";
                int diffMin = (int) DateUtil.between(sdf.parse(start), chargeCustomOrderInfo.getEndTime(), DateUnit.MINUTE);
                //该时段电量
                BigDecimal allPower = new BigDecimal(diffMin).multiply(powerMinute);
                //该时段电价
                BigDecimal electricityPriceTime = electricityPriceNow.multiply(allPower);
                //该时段服务费
                BigDecimal serviceChargeTime = serviceChargeNow.multiply(allPower);
                SumPeriod++;
                HashMap<String, Object> SumPeriodMap = new HashMap<>();
                SumPeriodMap.put("DetailsStartTime", startTime);//开始时间 格式（yyyy-MM-dd HH:mm:ss）
                SumPeriodMap.put("DetailsEndTime", endTime);//结束时间 格式（yyyy-MM-dd HH:mm:ss）
                SumPeriodMap.put("ElecPrice", electricityPriceNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段电价 小数点后4位 格式（0.00）
                SumPeriodMap.put("ServicePrice", serviceChargeNow.setScale(4, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后4位 格式（0.00）
                SumPeriodMap.put("DetailPower", allPower.setScale(2, BigDecimal.ROUND_HALF_UP));//时段充电量 单位:度 小数点后2位 格式（0.00）
                SumPeriodMap.put("DetailElecMoney", electricityPriceTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段电费 小数点后2位 格式（0.00）
                SumPeriodMap.put("DetailServiceMoney", serviceChargeTime.setScale(2, BigDecimal.ROUND_HALF_UP));//时段服务费 小数点后2位 格式（0.00）
                lists.add(SumPeriodMap);
                //总电价
                electricityPrice = electricityPrice.add(electricityPriceTime);
                //总服务费
                serviceCharge = serviceCharge.add(serviceChargeTime);
            }
        }

        returnMap.put("SumPeriod", SumPeriod);
        returnMap.put("ChargeDetails", lists);
        returnMap.put("electricityPrice",electricityPrice.setScale(2, BigDecimal.ROUND_HALF_UP));
        returnMap.put("serviceCharge",serviceCharge.setScale(2, BigDecimal.ROUND_HALF_UP));
        return returnMap;
    }


    /**
     * 每小时所占分钟数
     */
    public Map<String, Integer> getHourMinute(ChargeCustomOrderInfo chargeCustomOrderInfo) {
        Map<String, Integer> map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间
        Date start = chargeCustomOrderInfo.getStartTime();
        //结束时间
        Date end = chargeCustomOrderInfo.getEndTime();
        //时间段内所占小时
        List<String> dates = findDates(start, end);
        //开始时间与结束时间相差分钟
        int between = (int) DateUtil.between(start, end, DateUnit.MINUTE);
        //格式化时间
        String startTime = sdf.format(start);
        String endTime = sdf.format(end);
        //开始小时时间 60-分钟
        int startNumber = 60 - Integer.parseInt(startTime.substring(14, 16));
        //结束小时时间 分钟
        int endNumber = Integer.parseInt(endTime.substring(14, 16));
        //开始小时数
        String startHour = String.valueOf(Integer.parseInt(startTime.substring(11, 13)));
        //结束小时数
        String endHour = String.valueOf(Integer.parseInt(endTime.substring(11, 13)));
        if (dates.size() == 1) {
            map.put(startHour, between);
        } else if (dates.size() == 2) {
            map.put(startHour, startNumber);
            map.put(endHour, endNumber);
        } else {
            int startHours = Integer.parseInt(startHour) + 1;
            int endHours = Integer.parseInt(endHour);
            if (startHours > endHours) {
                endHours = endHours + 24;
            }
            map.put(startHour, startNumber);
            for (int i = startHours; i < endHours; i++) {
                if (i >= 24) {
                    map.put(String.valueOf(i - 24), 60);
                } else {
                    map.put(String.valueOf(i), 60);
                }
            }
            map.put(endHour, endNumber);
        }
        return map;
    }

    /**
     * 每小时所占秒数
     */
    public Map<String, Integer> getHourSeocnd(ChargeCustomOrderInfo chargeCustomOrderInfo) {
        Map<String, Integer> map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间
        Date start = chargeCustomOrderInfo.getStartTime();
        //结束时间
        Date end = chargeCustomOrderInfo.getEndTime();
        //时间段内所占小时
        List<String> dates = findDates(start, end);
        //开始时间与结束时间相差分钟
        int between = (int) DateUtil.between(start, end, DateUnit.SECOND);
        //格式化时间
        String startTime = sdf.format(start);
        String endTime = sdf.format(end);
        //开始小时时间 60-分钟
        int startNumber = (60 - Integer.parseInt(startTime.substring(14, 16))) * 60 + Integer.parseInt(startTime.substring(17, 19));
        //结束小时时间 分钟
        int endNumber = Integer.parseInt(endTime.substring(14, 16)) * 60 + Integer.parseInt(startTime.substring(17, 19));
        //开始小时数
        String startHour = String.valueOf(Integer.parseInt(startTime.substring(11, 13)));
        //结束小时数
        String endHour = String.valueOf(Integer.parseInt(endTime.substring(11, 13)));
        if (dates.size() == 1) {
            map.put(startHour, between);
        } else if (dates.size() == 2) {
            map.put(startHour, startNumber);
            map.put(endHour, endNumber);
        } else {
            int startHours = Integer.parseInt(startHour) + 1;
            int endHours = Integer.parseInt(endHour);
            if (startHours > endHours) {
                endHours = endHours + 24;
            }
            map.put(startHour, startNumber);
            for (int i = startHours; i < endHours; i++) {
                if (i >= 24) {
                    map.put(String.valueOf(i - 24), 60);
                } else {
                    map.put(String.valueOf(i), 60);
                }
            }
            map.put(endHour, endNumber);
        }
        return map;
    }

    /**
     * 每小时单价
     */
    public Map<String, Map<String, BigDecimal>> getHourPrice(String priceTypeCode) {
        Map<String, Map<String, BigDecimal>> map = new HashMap<>();
        ChargeStationPriceType chargeStationPriceType = chargeStationPriceTypeMapper.selectChargeStationPriceTypeByPriceTypeCode(priceTypeCode);
        BigDecimal foundationElectricCharge = chargeStationPriceType.getFoundationElectricCharge();
        BigDecimal foundationServiceFee = chargeStationPriceType.getFoundationServiceFee();
        ChargeStationPrice chargeStationPrice = new ChargeStationPrice();
        chargeStationPrice.setPriceTypeCode(priceTypeCode);
        List<ChargeStationPrice> chargeStationPrices = chargeStationPriceMapper.selectChargeStationPriceList(chargeStationPrice);
        Map<String, BigDecimal> mapFoundation = new HashMap();
        mapFoundation.put("electricCharge", foundationElectricCharge);
        mapFoundation.put("serviceFee", foundationServiceFee);
        for (int i = 0; i < chargeStationPrices.size(); i++) {
            ChargeStationPrice chargeStationPrice1 = chargeStationPrices.get(i);
            BigDecimal electricCharge = chargeStationPrice1.getElectricCharge();
            BigDecimal serviceFee = chargeStationPrice1.getServiceFee();
            String startTime = sdf.format(chargeStationPrice1.getStartTime());
            String endTime = sdf.format(chargeStationPrice1.getEndTime());
            int startNumber = Integer.parseInt(startTime.substring(0, 2));
            int endNumber = Integer.parseInt(endTime.substring(0, 2));
            Map mapNicety = new HashMap();
            mapNicety.put("electricCharge", electricCharge);
            mapNicety.put("serviceFee", serviceFee);
            while (startNumber <= endNumber) {
                map.put(String.valueOf(startNumber), mapNicety);
                startNumber = startNumber + 1;
            }
        }
        for (int i = 0; i < 23; i++) {
            if (!map.containsKey(String.valueOf(i))) {
                map.put(String.valueOf(i), mapFoundation);
            }
        }
        return map;
    }

    public List<Map<String, Object>> getHourPriceNew(String priceTypeCode) {
        List<Map<String, Object>> priceList = new ArrayList<>();
        ChargeStationPriceType chargeStationPriceType = chargeStationPriceTypeMapper.selectChargeStationPriceTypeByPriceTypeCode(priceTypeCode);
        if (chargeStationPriceType.getPriceTypeId() == null) {
            log.error("————————————————————————————交易订单保存，查询单价，模版未设置价格————————————————————————————> ");
            return priceList;
        }
        //查询价格信息
        ChargeStationPrice chargeStationPrice = new ChargeStationPrice();
        chargeStationPrice.setPriceTypeId(chargeStationPriceType.getPriceTypeId());
        List<ChargeStationAdjustPrice> chargeStationAdjustPrices = chargeStationAdjustPriceMapper.selectDevicePriceList(chargeStationPrice);
        ChargeStationAdjustPrice chargeStationAdjustPrice = chargeStationAdjustPrices.get(0);
        List<ChargeStationAdjustPriceAdditional> ruleJSONObject =
                JSONObject.parseArray(chargeStationAdjustPrice.getTableData(), ChargeStationAdjustPriceAdditional.class);
        for (ChargeStationAdjustPriceAdditional additional : ruleJSONObject) {
            String interval = additional.getInterval();
            String[] split = interval.split("],\\[");
            List<String> timeList = new ArrayList<>();
            for (String s : split) {
                String replace = s.replace("[", "").replace("\"", "").replace("]", "");
                Map<String, Object> addMap = new HashMap<String, Object>() {{
                    put("electricityPrice", additional.getElectricityPrice());
                    put("serviceCharge", additional.getServiceCharge());
                    put("time", replace);
                }};
                priceList.add(addMap);
            }
        }

        if (priceList.size() > 0) {
            //根据时间排序
            Collections.sort(priceList, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Integer hour1 = Integer.parseInt(String.valueOf(o1.get("time")).substring(0, 2));
                    Integer hour2 = Integer.parseInt(String.valueOf(o2.get("time")).substring(0, 2));
                    return hour1.compareTo(hour2);
                }
            });
        }

        return priceList;
    }

    /**
     * 时间段内所占小时
     */
    public static List<String> findDates(Date startTime, Date endTime) {
        List<String> lDate = new ArrayList<String>();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH");
        Calendar calStart = Calendar.getInstance();
        calStart.setTime(startTime);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(endTime);
        int startHours = calStart.get(Calendar.HOUR_OF_DAY);
        int endHours = calEnd.get(Calendar.HOUR_OF_DAY);
        if (startHours > endHours) {
            endHours = endHours + 24;
        }
        while (startHours <= endHours) {
            lDate.add(sd.format(calStart.getTime()));
            calStart.add(Calendar.HOUR_OF_DAY, 1);
            startHours = startHours + 1;
        }
        return lDate;
    }


}
